package com.dengqi.mymodule.activity.empty;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.dengqi.mymodule.BaseActivity;
import com.dengqi.mymodule.BuildConfig;
import com.dengqi.mymodule.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class GloadingActivity extends BaseActivity {

    protected Gloading.Holder mHolder;

    @BindView(R.id.button1)
    Button button1;
    @BindView(R.id.button2)
    Button button2;
    @BindView(R.id.button3)
    Button button3;
    @BindView(R.id.layout)
    LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gloading);
        ButterKnife.bind(this);

        Gloading.debug(BuildConfig.DEBUG);
        Gloading.initDefault(new GlobalAdapter());

        if (mHolder == null) {
            //bind status view to activity root view by default
            mHolder = Gloading.getDefault().wrap(layout).withRetry(new Runnable() {
                @Override
                public void run() {
                    onLoadRetry();
                }
            });
        }
    }

    @OnClick({R.id.button1, R.id.button2, R.id.button3, R.id.button4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button1:
                loadData();
                break;
            case R.id.button2:
                showLoadFailed();
                break;
            case R.id.button3:
                showEmpty();
                break;
            case R.id.button4:
                startActivity(new Intent(this, GloadingScrollActivity.class));
                break;
        }
    }

    private void loadData() {
        showLoading();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                showLoadSuccess();
            }
        }, 1500);
    }

    protected void onLoadRetry() {
        // override this method in subclass to do retry task
        loadData();
    }

    public void showLoading() {
        mHolder.showLoading();
    }

    public void showLoadSuccess() {
        mHolder.showLoadSuccess();
    }

    public void showLoadFailed() {
        mHolder.showLoadFailed();
    }

    public void showEmpty() {
        mHolder.showEmpty();
    }
}
